<?php

namespace App\Models;

use App\Log\SettingAdminLog;
use Illuminate\Database\Eloquent\Model;
use Saas,Cache;

class Setting extends Model
{
    const OPERATE_PID = 12;
    const MODEL = 'setting';
    /**
     * 关联到模型的数据表
     * @var string
     */
    protected $table = 'setting';

    protected $connection = 'mysql_official';


    /**
     * 表明模型是否应该被打上时间戳
     *
     * @var bool
     */
    public $timestamps = false;

    /**
     * 过滤掉新增字段
     * @var array
     */
    protected $guarded = [];

    /**
     * 设置系统配置
     */
    public static function setConfig(){
        $key = Saas::getConfigKey();
        if (Cache::has($key)) {
            config(['pin_setting'=>Cache::get($key)]);
        }else{
            $info = self::find(Saas::id());
            config(['pin_setting'=>$info->toArray()]);
            //Cache::put($key, $info, 60);
            Cache::forever($key,$info->toArray());
        }
    }

    public static function addLog($pre,$next,$type,$method='')
    {
        $info = OperateMoudel::where('pid',self::OPERATE_PID)->where('type',$type)->where('model',self::MODEL)->when($method,function ($query)use ($method){
            $query->where('method',$method);
        })->first();
        if (!empty($info)){
            $adminLog = new SettingAdminLog($pre,$next,$info);
            $adminLog->addLog();
        }
    }

    public static function add($data)
    {
        if(!is_array($data) || empty($data)) return false;

        $res=self::create($data);
        return $res->id;
    }

}
